package com.onlinescxhz.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.onlinescxhz.demo.common.Result;
import com.onlinescxhz.demo.pojo.GoodsSKU;
import com.onlinescxhz.demo.pojo.GoodsSPU;
import com.onlinescxhz.demo.pojo.ShopCart;
import com.onlinescxhz.demo.service.GoodsSKUServiceImpl;
import com.onlinescxhz.demo.service.GoodsSPUServiceImpl;
import com.onlinescxhz.demo.service.ShopCartServiceImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/shopcart")
public class ShopCartController {
    @Resource
    private ShopCartServiceImpl shopCartService;

    @Resource
    private GoodsSPUServiceImpl goodsSPUService;
    @Resource
    private GoodsSKUServiceImpl goodsSKUService;

    /**
     * 通过用户id 查询 用户购物车
     */
    @PostMapping("/cartlist")
    public List<ShopCart> selectbyuid(Integer uid){
        return shopCartService.list();
    }



    /**
     * 通过用户id 添加商品spu sku
     * @param uid
     * @param spuid
     * @param skuid
     * @return
     */
    @GetMapping("/addgoods")
    public Result addgoodcart(Integer uid,Integer spuid,Integer skuid){
        System.out.println(uid+":"+spuid+":"+skuid);
        QueryWrapper<GoodsSPU> queryWrapperspu = new QueryWrapper<>();
        queryWrapperspu.eq("id", spuid);
        GoodsSPU goodsSPU=goodsSPUService.getOne(queryWrapperspu);
        QueryWrapper<GoodsSKU> queryWrappersku = new QueryWrapper<>();
        queryWrappersku.eq("id",skuid);
        GoodsSKU goodsSKU=goodsSKUService.getOne(queryWrappersku);
        //System.out.println(goodsSKU.toString()+goodsSPU.toString());
        shopCartService.save(new ShopCart(0,uid,spuid,skuid,goodsSPU.getImgurl(),goodsSPU.getName(),goodsSKU.getDescribee(),1,goodsSKU.getPrice()));
        return Result.seccess("ok");
    }
}
